//此js文件包含取得树内容的函数

var xmlhttp; //为树函数所用
var refreshfinish = false;//刷新树全局标识
var nomsg = false;//弹出提示全局开关

//创建xmlhttp实例
function createxmlhttprequest(){
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				xmlhttp = false;
			}  
		}
	}
	if (!xmlhttp)
	alert("创建 Ajax 实例出错！");
}   

function delloadingtext(){
    if(innerPlace.tagName!="DIV"){
        innerPlace.getElementsByTagName("a")[1].innerHTML = innerPlace.getElementsByTagName("a")[1].innerHTML.substr(0,(innerPlace.getElementsByTagName("a")[1].innerHTML.length-loadingtext.length));
    }
	if(!refreshfinish){setTimeout('setting();',173);}
}
var onsuccess = "";
var onloading = "";
var onerror = "";
var loadingtext = "<span><img src='/javascripts/dep_manage/images/loaddot.gif' border=0 align=absmiddle></span>"
var AjaxRequestObj = "";
//封装 ajax 类
function Ajax(method,url,asynchronous,onsuccessFun,onloadingFun,onerrorFun){
    onsuccess = onsuccessFun;
    onloading = onloadingFun;
    onerror = onerrorFun;
    this.asynchronous = true;
    this.onloading=function(){onloadFun};
    this.onerror=function(){onerrorFun};
    createxmlhttprequest();
    xmlhttp.onreadystatechange = handleStateChange;
    xmlhttp.open(method,url,asynchronous);
    this.request = function(){xmlhttp.send(null);}
}
function handleStateChange(){
    if (xmlhttp.readyState==4){
        if(xmlhttp.status==200){
            AjaxRequestObj = xmlhttp.responseXML;
            eval(onsuccess);
        }else{
            if(xmlhttp.status==404)
            eval(onerror); //页面不存在
        }
    }else{ 
        if (xmlhttp.readyState==3)
            eval(onloading);
    }
}

var innerPlace="";
function innerloadingtext(obj){
    if(obj.parentNode.getElementsByTagName("a")[0].className=="open"){
        try{
            ulobj = obj.parentNode.getElementsByTagName("ul")[0];
        }catch(e){ulobj = "";}
        if(typeof(ulobj)!="object")
        obj.innerHTML += loadingtext;
    }
}
function switchclass(id,obj,level,isfirst){  //改变li的样式，从样式判断是否需要读取数据
    if(obj.className=="close"){
        obj.className="open"; 
        try{
            ulobj = obj.parentNode.getElementsByTagName("ul")[0];
        }catch(e){ulobj = "";}
        if(typeof(ulobj)=="object"){
            if(ulobj.style.display="none"){
                ulobj.style.display="block";
            }else{
                getValue(id,obj.parentNode,level,isfirst);
            }
        }else{
            getValue(id,obj.parentNode,level,isfirst);
        }
    }else{
        if(obj.className=="open"){obj.className="close";obj.parentNode.getElementsByTagName("ul")[0].style.display="none";}
    }
}
function getValue(id,obj,deplevel,isfirst){ //取值
    var ajaxObj = new Ajax("GET","/usermanage/deplist?id="+id+"&level="+deplevel+"&first="+isfirst,true,"getsuccess()","getting()","getfailed()");
    ajaxObj.request();

    innerPlace = obj;
	if(isfirst==1){dorefreshdep(id);}
}
function dorefreshdep(id){ //刷新部委用户目录树
	var theli = document.getElementById("dep"+id);
	if(theli==null){
		setTimeout('dorefreshdep('+id+')',666);
	}else{
		refreshdep(id);
	}
}
function getsuccess(){  //取值成功后
    var addr = AjaxRequestObj.getElementsByTagName("dep");
    var addrLen = addr.length;    
    if(addrLen==0){
        setTimeout("delloadingtext()",200)
        innerPlace.firstChild.className="nonedata";
    }else{
        var subnode="";
        var selfid;
        var name;
		var state;
        var url;
        var sonnum;
		var usernum;
		var fullname;
        for (i=0; i<addrLen; i++){
          selfid = addr[i].firstChild.data.split("|")[0];
          name = addr[i].firstChild.data.split("|")[1];
		  state = addr[i].firstChild.data.split("|")[2];
          url = addr[i].firstChild.data.split("|")[3];
          sonnum = addr[i].firstChild.data.split("|")[4];
		  usernum = addr[i].firstChild.data.split("|")[5];
		  fullname = addr[i].firstChild.data.split("|")[6];
		  if (state==1){
			  name = name+"本级";
		  }
          if(fullname!=undefined)
              subnode = subnode + "<li id=dep"+selfid+"><a id=a"+selfid+" href='javascript:void(0)' onclick=\"switchclass("+ selfid +",this,0,0);innerloadingtext(this.parentNode.getElementsByTagName('a')[1])\" class='close'> </a><a href='"+ url +"'  onclick=\"switchclass("+ selfid +",this.parentNode.getElementsByTagName('a')[0],0,0);innerloadingtext(this)\" oncontextmenu=\"showmenuie5(0,"+(parseInt(selfid)+200)+",'"+name+"');\" title ='"+fullname+"'>" + name +"("+ (sonnum-1) +")</a></li>";
          else
            if(sonnum!="0"){
			  subnode = subnode + "<li><a href='javascript:void(0)' onclick=\"switchclass("+ selfid +",this,1,0);innerloadingtext(this.parentNode.getElementsByTagName('a')[1])\" class='close'> </a><a id=b"+selfid+" href='"+ url +"'  onclick=\"switchclass("+ selfid +",this.parentNode.getElementsByTagName('a')[0],1,0);innerloadingtext(this)\" oncontextmenu=\"showmenuie5(1,"+selfid+",'"+name+"');\" ";
			  switch(parseInt(state)){
			    case 1:subnode = subnode + " class='benji'";break;
				case 3:subnode = subnode + " class='noregist'";break;
				default:;
			  }
			  subnode = subnode + ">" + name +"("+ sonnum +")</a></li>";
			}
            else{
			  subnode = subnode + "<li><a href='javascript:void(0)' class='nonedata'> </a><a id=b"+selfid+" href='"+ url +"' oncontextmenu=\"showmenuie5(1,"+selfid+",'"+name+"');\"";
			  switch(parseInt(state)){
				case 1:subnode = subnode + " class='benji'";break;
				case 3:subnode = subnode + " class='noregist'";break;
				default:;
			  }
			  subnode = subnode + ">" + name + "</a></li>";
			}
        }
        subnode = "<ul>" + subnode + "</ul>";
        setTimeout("delloadingtext()",200)
		//if (typeof(document.all.loading)=="object"){delloadingpic();}
		if (document.all.loading!=null){delloadingpic();}
        innerPlace.innerHTML = innerPlace.innerHTML + subnode;
		//alert(document.getElementById("tree").innerHTML);
    //document.getElementById("inner").value = document.getElementById("tree").innerHTML
    }
}
function setting(){refreshfinish = true;}
function getting(){ //正在取值时
//    innerPlace.innerHTML = "Loading...";
}
function getfailed(){ //取值失败
    alert("err")
}

//导入其他js脚本
var head = document.getElementsByTagName("head")[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "/javascripts/dep_manage/find.js";
head.appendChild(script);
script = document.createElement('script');
script.type = 'text/javascript';
script.src = "/javascripts/dep_manage/detail.js";
head.appendChild(script);
script = document.createElement('script');
script.type = 'text/javascript';
script.src = "/javascripts/dep_manage/adv.js";
head.appendChild(script);
script = document.createElement('script');
script.type = 'text/javascript';
script.src = "/javascripts/dep_manage/modi.js";
head.appendChild(script);
script = document.createElement('script');
script.type = 'text/javascript';
script.src = "/javascripts/dep_manage/user.js";
head.appendChild(script);
script = document.createElement('script');
script.type = 'text/javascript';
script.src = "/javascripts/dep_manage/charge.js";
head.appendChild(script);
script = document.createElement('script');
script.type = 'text/javascript';
script.src = "/javascripts/dep_manage/msg.js";
head.appendChild(script);
script = document.createElement('script');
script.type = 'text/javascript';
if(userlevel==1){
script.src = "/javascripts/dep_manage/menu0.js";
}else{
script.src = "/javascripts/dep_manage/menu1.js";
}
head.appendChild(script);
script = document.createElement('script');
script.type = 'text/javascript';
script.src = "/javascripts/dep_manage/move.js";
head.appendChild(script);
